package com.xzj.admin.analysis.base.custom.common;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.common.cache.RegionPlatformCache;
import com.xzj.admin.analysis.base.common.redis.ICustomRedisHandle;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.analysis.base.domain.CommonFlowInfo;
import com.xzj.admin.analysis.base.util.AnalysisCommonUtil;

/**
 * @program: xzj-admin
 * @ClassName: PlatformSpecialHandle
 * @description: 虚拟化云平台特殊处理类
 * @author: XiaZunJun
 * @create: 2024-11-20 11:35
 */
public class PlatformSpecialHandle {

    protected static final ICustomRedisHandle<String> redisHandle = SpringUtil.getBean(ICustomRedisHandle.class);

    public static void datacenterAdd(ResMessageManage message, CommonFlowInfo info){
        String relatePlatformKey = AnalysisCommonUtil.commonBuildStr(info.getCacheKeyPrefix(), "relatePlatform");
        JSONObject row = message.getRowData();
        row.put("platform_id", redisHandle.getCacheMapValue(relatePlatformKey, "platformId"));
        message.setMessageJson(row.toJSONString());
    }

    public static void regionAdd(ResMessageManage message, CommonFlowInfo info){
        String relatePlatformKey = AnalysisCommonUtil.commonBuildStr(info.getCacheKeyPrefix(), "relatePlatform");
        JSONObject row = message.getRowData();
        String endpoint = row.getString("endpoint");
        if (StrUtil.isNotEmpty(endpoint) && (endpoint.contains(":"))) {
            endpoint = endpoint.substring(0, endpoint.indexOf(":"));
        }
        RegionPlatformCache.putRegionIpRelation(endpoint, info.getRmPoolId());
        redisHandle.setCacheMapValue(relatePlatformKey, "platformId", RegionPlatformCache.getPlatformIpRelation(info.getRmPoolId()));
        redisHandle.setCacheMapValue(relatePlatformKey, "resourcePoolId", RegionPlatformCache.platformResourcePoolId(info.getRmPoolId()));
        String baseKey = AnalysisCommonUtil.commonBuildStr(info.getCacheKeyPrefix(), "RegionIp");
        redisHandle.setCacheMapValue(baseKey, info.getRmPoolId(), endpoint);
    }

    public static void vmOrHostAdd(ResMessageManage message, CommonFlowInfo info){
        String relatePlatformKey = AnalysisCommonUtil.commonBuildStr(info.getCacheKeyPrefix(), "relatePlatform");
        JSONObject row = message.getRowData();
        row.put("resourcePoolId", redisHandle.getCacheMapValue(relatePlatformKey, "resourcePoolId"));
        message.setMessageJson(row.toJSONString());
    }
}
